import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Day2 {
    //1.牛牛的快递
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            double a = in.nextDouble();
            char b = in.next().charAt(0);
            double count=0;
            if(b == 'y') {
                count+=5;
            }
            if(a>0)
                count+=20;
            a--;
            while(a-->0) {
                count+=1;
            }
            System.out.printf("%.0f",count);
        }
    }
    //2.最小花费爬楼梯

    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int[] cost = new int[n];
            for(int i=0;i<n;i++) cost[i]=in.nextInt();
            int[] dp = new int[n+1];
            dp[0] = dp[1] = 0;
            for(int i=2;i<n+1;i++) dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);

            System.out.println(dp[n]);
        }
    }


    //3.数组中两个字符串的最小距离
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String str1 = in.next();
            String str2 = in.next();
            String[] strs = new String[n];
            for(int i=0;i<n;i++) {
                strs[i]=in.next();
            }
            boolean bool = true;
            int[] a = {-1,-1};
            int len = Integer.MAX_VALUE;
            for(int i=0;i<n;i++) {
                String tmp = strs[i];
                if(a[0]!=-1 && a[1]!=-1) {
                    len = Math.min(len,Math.abs(a[0]-a[1]));
                    bool = false;
                }
                if(tmp.equals(str1)) {
                    a[0]=i;
                }
                if(tmp.equals(str2)) {
                    a[1]=i;
                }
            }
            if(bool) {
                System.out.println(-1);
            }else {
                System.out.println(len);

            }

        }
    }
}
